export {
    splitAddress
}

function splitAddress(fullAddress) {
    let province = '', city = '', district = '', street = '';
    let remainingAddress = fullAddress.replace(/,/g, '');
    // console.log("去除逗号：",remainingAddress);

    // 使用正则表达式提取省份（更精确的匹配）
    const provincePattern = /(.*?(?:省|自治区|特别行政区))/;
    const provinceMatch = remainingAddress.match(provincePattern);
    if (provinceMatch) {
        province = provinceMatch[1];
        remainingAddress = remainingAddress.substring(province.length);
    } else {
        // 如果没有省份后缀，但以直辖市开头，则作为省份处理
        const directMunicipalityPattern = /(^(?:北京|上海|天津|重庆)市?)/;
        const municipalityMatch = remainingAddress.match(directMunicipalityPattern);
        if (municipalityMatch) {
            province = municipalityMatch[1] + (municipalityMatch[1].endsWith('市') ? '' : '市');
            remainingAddress = remainingAddress.substring(municipalityMatch[1].length);
        }
    }

    // 提取城市（匹配完整的行政区划名称）
    const cityPattern = /(.*?(?:市|地区|自治州|盟))/;
    const cityMatch = remainingAddress.match(cityPattern);
    if (cityMatch) {
        city = cityMatch[1];
        remainingAddress = remainingAddress.substring(city.length);
    }

    // 提取区县（匹配完整的行政区划名称，避免误匹配社区、小区等地名）
    const districtPattern = /^(.*?(?:区|县|自治县|旗|自治旗|县级市|林区|特区))/;
    const districtMatch = remainingAddress.match(districtPattern);
    if (districtMatch) {
        district = districtMatch[1];
        remainingAddress = remainingAddress.substring(district.length);
    }

    // 剩余部分为街道
    street = remainingAddress;

    // 返回区域部分和街道部分
    const region = [province, city, district].filter(Boolean).join('');
    return [region, street];
}